package com.qingxin.cms.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.cms.entity.CmsCategory;
import com.qingxin.cms.service.CmsCategoryService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.utils.StringUtils;


/**
 * 文章类别管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/cms/category")
public class CmsCategoryAction extends BaseAction {
	
	@Autowired
	private CmsCategoryService cmsCategoryService;
	
	
	/**
	 * 加载左侧 类别-文章
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/lefttree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public List<JsTreeNode> lefttree(String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		if(StringUtils.isEmpty(pid)){
			JsTreeNode root = new JsTreeNode("0", "所有类别","CATEGORY", null,true);
			list.add(root);
		}else{
			list.addAll(cmsCategoryService.getUITreeData(getLoginUser(request)));
		}
		return list;
	}
	
	/**
	 * 列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/cms/category/category-list";
	}
	/**
	 * 加载类别列表分页数据
	 * @param search
	 * @param leftdate
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String search,String leftdate, PageParam pageParam, HttpServletRequest request){
		return cmsCategoryService.list(pageParam,search,leftdate,getLoginUser(request),request);
	}
	
	/**
	 * 添加类别
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		switch (getLoginUser(request).getUser_type()) {
		case "0":
			model.put("loginUserType", "0");
			break;
		case "1":
			model.put("loginUserType", "0");
			break;
		case "2":
			model.put("loginUserType", "1");
			break;
		default:
			model.put("loginUserType","0" );
			break;
		}
		return "/trace/cms/category/category_add";
	}
	
	/**
	 * 保存类别信息
	 * @param category
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid CmsCategory category ,BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = cmsCategoryService.add(category,getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 修改类别
	 * @param model
	 * @param categoryId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String categoryId, HttpServletRequest request){
		CmsCategory category = cmsCategoryService.get(categoryId);
		model.put("category", category);
		return "trace/cms/category/category_edit";
	}
	
	/**
	 * 修改保存
	 * @param category
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid CmsCategory category, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		return cmsCategoryService.update(category, getLoginUser(request), request);
	}
	
	/**
	 * 删除类别
	 * @param categoryId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] categoryId,HttpServletRequest request){
		if(categoryId==null || categoryId.length==0){
			return new SysError("没有选择任何信息.");
		}
		return cmsCategoryService.delete(categoryId, getLoginUser(request), request);
	}
	
	
}
